<?php

namespace Modules\Admin\Http\Controllers\Categories;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Modules\Common\General\Security\Api\Gateway;
use Modules\Common\Models\AdminLog;
use Modules\Common\Models\Categories;
use Modules\Common\Models\Ebooks;
use Modules\Common\Models\Helpers\TrashManager;
use Modules\Common\Util\Api;
use Modules\Common\Util\Database;
use Modules\Common\Exceptions\Base\Exception;


class Delete
{
    /**
     * @Route(path=/api/admin/category  method=delete)
     */
    public function index()
    {

        DB::transaction(function () {

            $category = Categories::find(request('id'));
            if ($category == null) {
                Exception::new()
                    ->tellUser('分类不存在')
                    ->logNotice('分类不存在', ['request' => request()->all()])
                    ->throw();
            }
            //开始记录删除操作，添加至回收站
            TrashManager::startTrace(sprintf('删除分类：%s', $category->category_name));

            // $ebooksCount = Ebooks::where('category_id', '=', $category->id)->count();

            // if ($ebooksCount > 0) {
            //     Exception::new()
            //         ->tellUser('已产生电子书关联该分类，不可删除')
            //         ->logNotice('已产生电子书关联该分类，不可删除', ['request' => request()->all()])
            //         ->throw();
            // }
            
            //修改电子书关联分类的状态
             $updateEbooksCategory = Ebooks::where('category_id', '=', $category->id)->update(['category_id'=>0]);

            // 删除操作
            $category->delete();

            TrashManager::endTrace();
        });


        return Api::success();
    }
    
    
    /**
     * @Route(path=/api/admin/category-batch-delete  method=delete)
     */
    public function index2()
    {
        // 假设请求中传递了一个包含多个分类 ID 的数组，例如：ids=1,2,3
        $categoryIds = request('ids');
        
        // 将字符串转换为数组
        $categoryIdsArray = explode(',', $categoryIds);
    
        // 启动数据库事务
        DB::transaction(function () use ($categoryIdsArray) {
            foreach ($categoryIdsArray as $categoryId) {
                $category = Categories::find($categoryId);
                if ($category == null) {
                    Exception::new()
                        ->tellUser('分类不存在')
                        ->logNotice('分类不存在', ['request' => request()->all()])
                        ->throw();
                }
                // 开始记录删除操作，添加至回收站
                TrashManager::startTrace(sprintf('删除分类：%s', $category->category_name));
    
                // 检查是否有电子书关联该分类
                // $ebooksCount = Ebooks::where('category_id', '=', $categoryId)->count();
                // if ($ebooksCount > 0) {
                //     Exception::new()
                //         ->tellUser('已产生电子书关联该分类，不可删除')
                //         ->logNotice('已产生电子书关联该分类，不可删除', ['request' => request()->all()])
                //         ->throw();
                // }
                
                // 修改电子书关联分类的状态
                $updateEbooksCategory = Ebooks::where('category_id', '=', $categoryId)->update(['category_id' => 0]);
    
                // 删除操作
                $category->delete();
    
                TrashManager::endTrace();
            }
        });
         return Api::success();
    }

}